home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / comm / term / vltj5867.lha / VLT / rexx / X10Direct.vlt < prev   
Text File  |  1994-03-27  |  6KB  |  237 lines

  1. /** X10Direct.vlt
  2. *
  3. *   This program brings up a requester which allows you to send commands
  4. *   to X10 modules.
  5. *
  6. *   By W.G.J. Langeveld, July 1992.
  7. *
  8. *   This program really wants to be called *from inside* VLT.
  9. *
  10. *   Below is a sample house code, and examples for the various units
  11. *   for that housecode. Defaults are set up for all other house codes.
  12. *
  13. **/
  14. do i = 1 to 16
  15.    do j = 0 to 15
  16.       hc = substr("ABCDEFGHIJKLMNOP", i, 1)
  17.       gtext.hc.j = "Unit "left(j, 2)"                              "
  18.    end
  19. end
  20.  
  21. housecode = "L"
  22.  
  23. gtext.L.0  = "Unit 1  Living room light            "
  24. gtext.L.1  = "Unit 2  Kitchen light                "
  25. gtext.L.2  = "Unit 3  Dining Room light            "
  26. gtext.L.3  = "Unit 4  Bedroom Light                "
  27. /*
  28. *   Add the libraries if they're not there yet.
  29. */
  30. if show("l", "rexxarplib.library") = 0 then do
  31.    check = addlib('rexxsupport.library', 0, -30, 0)
  32.    check = addlib('rexxarplib.library',  0, -30, 0)
  33. end
  34. /*
  35. *   The following sequence determines both VLT's screen and port name,
  36. *   and also gets the number screen rows and columns.
  37. */
  38. vltport = address()
  39. cols = ScreenCols(vltport)
  40. if cols == -1 then do
  41.    vltscreen = ""
  42.    cols = ScreenCols()
  43.    rows = ScreenRows()
  44. end
  45. else do
  46.    vltscreen = vltport
  47.    rows = ScreenRows(vltscreen)
  48. end
  49. /*
  50. *   Set up a host. This time, send all messages to our own port.
  51. */
  52. address AREXX "'x = CreateHost(VLTX10DIRECT, VLTX10PORT, "vltscreen")'"
  53. /*
  54. *   Wait until it is ready.
  55. */
  56. do i = 1
  57.    if showlist('p', VLTX10DIRECT) ~= 0 then leave
  58.    call delay 30
  59. end
  60. /*
  61. *   Open the window
  62. */
  63. idcmp = 'CLOSEWINDOW+GADGETUP'
  64. flags = 'WINDOWCLOSE+WINDOWDRAG+WINDOWDEPTH+ACTIVATE'
  65.  
  66. call SetReqColor(VLTX10DIRECT, BACKGROUNDPEN, 0)
  67.  
  68. call OpenWindow(VLTX10DIRECT, (cols - 640)/2, rows - 170, 640, 170, idcmp,   ,
  69.                 flags, "X10 Control Panel")
  70. /*
  71. *   Open the port that messages will come to
  72. */
  73. mp = openport(VLTX10PORT)
  74. /*
  75. *   Add the gadgets
  76. */
  77. call MakeGadgets(housecode)
  78. /*
  79. *   Set VLT for 600 bps
  80. */
  81. "extract baud"
  82. lastbaud = VLT.baud
  83. "baud midi 600"
  84. /*
  85. *   Main loop
  86. */
  87. x10state. = 0
  88.  
  89. quitflag  = 0
  90.  
  91. do forever
  92.    if quitflag = 1 then leave
  93.    t = waitpkt(VLTX10PORT)
  94.    do forever
  95.       p = getpkt(VLTX10PORT)
  96.       if c2d(p) = 0 then leave
  97.  
  98.       cmd = getarg(p)
  99.  
  100.       t = reply(p, 0)
  101. /*
  102. *   See what we got
  103. */
  104.       parse var cmd cmd mousex .
  105.  
  106.       if cmd = "CLOSEWINDOW" then do
  107. /*
  108. *   Reset VLT's baud rates.
  109. */
  110.          "baud "lastbaud
  111.  
  112.          call CloseWindow(VLTX10DIRECT)
  113.          quitflag = 1
  114.       end
  115.       else if cmd = "HC" then do
  116.          res = Request(100, screenrows(vltscreen) - 150, "Enter new house code", ,
  117.                        housecode, , "Cancel", vltscreen)
  118.          if res ~= "" then do
  119.             res = upper(substr(res, 1, 1))
  120.             if index("ABCDEFGHIJKLMNOP", res) = 0 then do
  121.                call PostMsg(100, screenrows(vltscreen) - 150,    ,
  122.                             "Invalid hose code: "res, vltscreen)
  123.                call Delay(100)
  124.                call PostMsg()
  125.             end
  126.             else if res ~= housecode then do
  127.                housecode = res
  128.                do i = 0 to 15
  129.                   call RemoveGadget(VLTX10DIRECT, i)
  130.                end
  131.                call MakeGadgets(housecode)
  132.             end
  133.          end
  134.       end
  135.       else if datatype(cmd, 'W') = 1 then do
  136.          if cmd < 8 then mousex = mousex - 10
  137.          else            mousex = mousex - 324
  138.  
  139.          if mousex > 150 then call SendCommand(housecode, cmd, "on")
  140.          else                 call SendCommand(housecode, cmd, "off")
  141.       end
  142.    end
  143. end
  144. exit
  145.  
  146.  
  147. MakeGadgets: procedure expose gtext.
  148.  
  149. parse arg hc
  150.  
  151. call SetAPen(VLTX10DIRECT, 0)
  152. call RectFill(VLTX10DIRECT, 5, 15, 633, 166)
  153.  
  154. prompts =   ,
  155.   "Off                             On  || Off                             On"
  156. call WindowText(VLTX10DIRECT, prompts)
  157.  
  158. call AddGadget(VLTX10DIRECT,  10,  32,  0, gtext.hc.0 , "%d %x")
  159. call AddGadget(VLTX10DIRECT,  10,  46,  1, gtext.hc.1 , "%d %x")
  160. call AddGadget(VLTX10DIRECT,  10,  60,  2, gtext.hc.2 , "%d %x")
  161. call AddGadget(VLTX10DIRECT,  10,  74,  3, gtext.hc.3 , "%d %x")
  162. call AddGadget(VLTX10DIRECT,  10,  88,  4, gtext.hc.4 , "%d %x")
  163. call AddGadget(VLTX10DIRECT,  10, 102,  5, gtext.hc.5 , "%d %x")
  164. call AddGadget(VLTX10DIRECT,  10, 116,  6, gtext.hc.6 , "%d %x")
  165. call AddGadget(VLTX10DIRECT,  10, 130,  7, gtext.hc.7 , "%d %x")
  166.  
  167. call AddGadget(VLTX10DIRECT, 324,  32,  8, gtext.hc.8 , "%d %x")
  168. call AddGadget(VLTX10DIRECT, 324,  46,  9, gtext.hc.9 , "%d %x")
  169. call AddGadget(VLTX10DIRECT, 324,  60, 10, gtext.hc.10, "%d %x")
  170. call AddGadget(VLTX10DIRECT, 324,  74, 11, gtext.hc.11, "%d %x")
  171. call AddGadget(VLTX10DIRECT, 324,  88, 12, gtext.hc.12, "%d %x")
  172. call AddGadget(VLTX10DIRECT, 324, 102, 13, gtext.hc.13, "%d %x")
  173. call AddGadget(VLTX10DIRECT, 324, 116, 14, gtext.hc.14, "%d %x")
  174. call AddGadget(VLTX10DIRECT, 324, 130, 15, gtext.hc.15, "%d %x")
  175.  
  176. call AddGadget(VLTX10DIRECT, 324, 150, 20, "Change House Code", "HC")
  177.  
  178. call Move(VLTX10DIRECT, 10, 157)
  179. call SetAPen(VLTX10DIRECT, 1)
  180. call Text(VLTX10DIRECT, "Current house code: "hc)
  181.  
  182. return
  183.  
  184. SendCommand: procedure
  185. arg housecode, unit, cmd
  186. /*
  187. *   Clear screen
  188. */
  189.    "clear"
  190. /*
  191. *   Some data strings we'll use.
  192. */
  193.    syncstring = copies("*xFF", 16)
  194.    houseseq   = "MECKOGAINFDLPHBJ"
  195. /*
  196. *   Start calculating the checksum by using the housecode byte.
  197. */
  198.    cs = (index(houseseq, housecode) - 1) * 16
  199.    hc = "*x" || d2x(cs, 2)
  200.  
  201.    command.17 = "*x01"
  202.  
  203.    if upper(cmd) = "OFF" then do
  204.       command.18 = "*x03"
  205.       cs = cs + 3
  206.       call SetGadget("VLTX10DIRECT", unit, "OFF")
  207.    end
  208.    else if upper(cmd) = "ON" then do
  209.       command.18 = "*x02"
  210.       cs = cs + 2
  211.       call SetGadget("VLTX10DIRECT", unit, "ON")
  212.    end
  213.  
  214.    command.19 = hc
  215.  
  216.    if unit < 8 then do
  217.       command.20 = "*x00"
  218.       active = 21
  219.    end
  220.    else do
  221.       command.21 = "*x00"
  222.       active = 20
  223.       unit = unit - 8
  224.    end
  225.  
  226.    command.active = "*x" || d2x(2 ** (7 - unit), 2)
  227.  
  228.    cs = cs + 2 ** (7 - unit)
  229.    cs = cs // 256
  230.  
  231.    command.22 = "*x" || d2x(cs, 2)
  232.  
  233.    "send (" || syncstring || command.17 || command.18 || command.19 || ,
  234.                              command.20 || command.21 || command.22 || ")"
  235.    "wait ("hc") 3"
  236.    return
  237.